package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: AddressBookServiceImpl
 * Package: com.sky.service.impl
 * Description:
 *
 * @Autehor 屈子岩
 * @Create 2024/10/13 14:51
 * @Version 1.0
 */
@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;


    private static final Integer DEFAULT_ADDRESS_ID = 1;
    private static final Integer NOTDEFAULT_ADDRESS_ID = 0;

    @Override
    public void addLocation(AddressBook addressBook) {
        Long userId = BaseContext.getCurrentId();
        addressBook.setIsDefault(NOTDEFAULT_ADDRESS_ID);
        addressBook.setUserId(userId);
        addressBookMapper.insert(addressBook);
    }

    /**
     * 设置默认地址
     * @param addressBook
     */
    @Override
    public void defaultAddress(AddressBook addressBook) {
        addressBook.setIsDefault(DEFAULT_ADDRESS_ID);
        addressBookMapper.update(addressBook);
    }

    /**
     * 修改地址信息
     * @param addressBook
     */
    @Override
    public void update(AddressBook addressBook) {
        addressBookMapper.update(addressBook);
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @Override
    public AddressBook getById(Long id) {
        AddressBook addressBook = new AddressBook();
        addressBook.setId(id);
       List<AddressBook>  addressBookData = addressBookMapper.select(addressBook);
        AddressBook addressBook1 = addressBookData.get(0);
        return addressBook1;
    }

    /**
     * 根据id删除地址
     * @param id
     */
    @Override
    public void delete(Long id) {
        addressBookMapper.delecte(id);
    }

    /**
     * 查询当前登录用户的所有地址信息
     * @param addressBook
     * @return
     */
    @Override
    public List<AddressBook> list(AddressBook addressBook) {
        List<AddressBook> addressBookData = addressBookMapper.select(addressBook);
        return addressBookData;
    }

    @Override
    public List<AddressBook> defaultAddressquery(AddressBook addressBook) {
        addressBook.setIsDefault(DEFAULT_ADDRESS_ID);
        List<AddressBook> addressBookList = addressBookMapper.select(addressBook);
        return addressBookList;
    }

}
